<?php
namespace App\Services;

use App\exption\push\Hpush\Hpush;
use App\exption\push\Jpush\Jpush;
use App\exption\push\Xmpush\Xmpush;

final class PushService implements \App\interf\Push
{

    const CHANNEL_JG = 1;   // 极光
    const CHANNEL_HW = 2;   // 华为
    const CHANNEL_XM = 3;   // 小米
    private $pushObj;

    /*
     * 单个推送
     * */


    /*
     * 策略选择推送对象
     * */
    public function initInstance($params)
    {
        if ($params['channel_id'] == self::CHANNEL_JG) {
            $this->pushObj = new Jpush();
        } elseif ($params['channel_id'] == self::CHANNEL_HW) {
            $this->pushObj = new Hpush();
        } elseif ($params['channel_id'] == self::CHANNEL_XM) {
            $this->pushObj = new Xmpush();
        } else {
            $this->pushObj = null;
        }
    }

    /*
     * 单个推送
     * */
    public function singlePush($params)
    {
        if (is_null($this->pushObj))
            return ['status'=>'error', 'message_code'=>10159, 'message'=>'初始化错误', 'data'=>$params];
        if( !is_array($params) || !array_key_exists('title', $params) || !array_key_exists('content', $params) || !array_key_exists('description', $params) ||
            !array_key_exists('mobile_list', $params))
            return ['status'=>'error', 'message'=>'参数异常', 'data'=>$params];
        return $this->pushObj->singlePush($params);
    }

    /*
     * 批量推送
     * */
    public function batchPush($params)
    {
        if (is_null($this->pushObj))
            return ['status'=>'error', 'message_code'=>10159, 'message'=>'初始化错误', 'data'=>$params];
        if( !is_array($params) || !array_key_exists('title', $params) || !array_key_exists('content', $params) || !array_key_exists('description', $params) ||
            !array_key_exists('mobile_list', $params) || !is_array($params['mobile_list']))
            return ['status'=>'error', 'message_code'=>10128, 'message'=>'参数异常', 'data'=>$params];

        return $this->pushObj->batchPush($params);
    }
}
